{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "学习目标\n",
    "- 知道什么是矩阵和向量\n",
    "- 知道矩阵的加法,乘法\n",
    "- 知道矩阵的逆和转置\n",
    "- 应用np.matmul、np.dot实现矩阵运算"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "767ee978140060e1"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 1 矩阵和向量\n",
    "## 1.1 矩阵\n",
    "矩阵，英文matrix，和array的区别矩阵必须是2维的，但是array可以是多维的。\n",
    "矩阵的维数即行数×列数\n",
    "矩阵元素(矩阵项): \n",
    "$$A=\n",
    "\\begin{bmatrix}\n",
    "a & b & c & d  \\\\\n",
    "e & f & g & h  \\\\\n",
    "i & j & k & l  \\\\\n",
    "m & n & o & p\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "Aij 指第 i 行，第 j 列的元素。\n",
    "## 1.2 向量\n",
    "向量是一种特殊的矩阵，讲义中的向量一般都是列向量，下面展示的就是三维列向量(3×1)。\n",
    "$$A=\n",
    "\\begin{bmatrix}\n",
    "1 \\\\\n",
    "2 \\\\\n",
    "3 \\\\\n",
    "\\end{bmatrix}\n",
    "$$"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cab0f33aced47339"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2 加法和标量乘法\n",
    "## 矩阵的加法:行列数相等的可以加。\n",
    "例：\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 2\\\\\n",
    "3 & 4\\\\\n",
    "5 & 6\\\\\n",
    "\\end{bmatrix}\n",
    "+\n",
    "\\begin{bmatrix}\n",
    "1 & 2\\\\\n",
    "3 & 4\\\\\n",
    "5 & 6\\\\\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "2 & 4\\\\\n",
    "6 & 8\\\\\n",
    "10 & 12\\\\\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "## 矩阵的乘法:每个元素都要乘。\n",
    "例:\n",
    "$$\n",
    "3 *\n",
    "\\begin{bmatrix}\n",
    "1 & 2\\\\\n",
    "3 & 4\\\\\n",
    "5 & 6\\\\\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "3 & 6\\\\\n",
    "9 & 12\\\\\n",
    "15 & 18\\\\\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "组合算法也类似。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cd413fd956048f1"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 矩阵乘法\n",
    "## 3.1 矩阵向量乘法\n",
    "矩阵和向量的乘法如图：m×n 的矩阵乘以 n×1 的向量，得到的是 m×1 的向量\n",
    "例:\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 3\\\\\n",
    "4 & 0\\\\\n",
    "2 & 1\\\\\n",
    "\\end{bmatrix}\n",
    "*\n",
    "\\begin{bmatrix}\n",
    "1\\\\\n",
    "5\\\\\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "16\\\\\n",
    "4\\\\\n",
    "7\\\\\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "```\n",
    "1*1+3*5 = 16\n",
    "4*1+0*5 = 4\n",
    "2*1+1*5 = 7\n",
    "```\n",
    "矩阵乘法遵循准则：\n",
    "\n",
    "(M行, N列)*(N行, L列) = (M行, L列)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "deff6b591b98cec1"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 矩阵和矩阵乘法\n",
    "矩阵乘法：\n",
    "m×n 矩阵乘以 n×o 矩阵，变成 m×o 矩阵。\n",
    "举例：比如说现在有两个矩阵 A 和 B，那 么它们的乘积就可以表示为图中所示的形式。\n",
    "$$\n",
    "C=A     *   B\\\\\n",
    "\\begin{bmatrix}\n",
    "C0 & C1\\\\\n",
    "C2 & C3\\\\\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "A0 & A1\\\\\n",
    "A2 & A3\\\\\n",
    "\\end{bmatrix}\n",
    "*\n",
    "\\begin{bmatrix}\n",
    "B0 & B1\\\\\n",
    "B2 & B3\\\\\n",
    "\\end{bmatrix}\\\\\n",
    "C0 = A0 * B0 + A1 * B2\\\\\n",
    "C1 = A0 * B1 + A1 * B3\\\\\n",
    "C2 = A2 * B0 + A3 * B2\\\\\n",
    "C3 = A2 * B1 + A3 * B3\\\\\n",
    "$$"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fcf5b05df9cccbde"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.3 矩阵乘法的性质\n",
    "矩阵的乘法不满⾜交换律：A×B≠B×A\n",
    "矩阵的乘法满⾜结合律。即：A×（B×C）=（A×B）×C\n",
    "单位矩阵：在矩阵的乘法中，有⼀种矩阵起着特殊的作⽤，如同数的乘法中的 1,我\n",
    "们称 这种矩阵为单位矩阵．它是个⽅阵，⼀般⽤ I 或者 E 表示，从 左上⻆到右下\n",
    "⻆的对⻆线（称为主对⻆线）上的元素均为 1 以外全都为 0。如:\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 0\\\\\n",
    "0 & 1\\\\\n",
    "\\end{bmatrix}\\tag{2*2}\n",
    "\\\\\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 0\\\\\n",
    "0 & 1 & 0\\\\\n",
    "0 & 0 & 1\\\\\n",
    "\\end{bmatrix}\\tag{3*3}\n",
    "\\\\\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 0 & 0\\\\\n",
    "0 & 1 & 0 & 0\\\\\n",
    "0 & 0 & 1 & 0\\\\\n",
    "0 & 0 & 0 & 1\\\\\n",
    "\\end{bmatrix}\\tag{4*4}\n",
    "$$"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1a16270905e2ccfb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4 逆、转置\n",
    "矩阵的逆：如矩阵 A 是⼀个 m×m 矩阵（⽅阵），如果有逆矩阵，则：\n",
    "AA^-1 = A^-1 A = I (单位矩阵)\n",
    "- 低阶矩阵求逆的方法:\n",
    "    - 1.待定系数法\n",
    "    - 2.初等变换\n",
    "    - ......\n",
    "逆矩阵是针对一个方阵而言的，对于一个方阵 A，如果存在另一个方阵 B，使得 A 与 B 相乘等于单位矩阵 I，且 B 与 A 相乘也等于单位矩阵 I，则我们称 B 是 A 的逆矩阵，记作 A^-1。\n",
    "\n",
    "在Markdown中编写逆矩阵的公式可以使用LaTeX语法，LaTeX是一种专业的排版语言，通常用于书籍、论文、数学公式等的排版。\n",
    "\n",
    "以下是逆矩阵的公式以及Markdown中的写法：\n",
    "\n",
    "1. 逆矩阵的公式：\n",
    "\\[ A \\times A^{-1} = A^{-1} \\times A = I \\]\n",
    "这表示了方阵 A 乘以其逆矩阵等于单位矩阵。\n",
    "\n",
    "2. 在Markdown中的写法：\n",
    "```\n",
    "\\[ A \\times A^{-1} = A^{-1} \\times A = I \\]\n",
    "```\n",
    "在Markdown中使用 \\[ 和 \\] 包裹公式可以让Markdown渲染引擎知道这是一个数学公式的起始和结束。在公式中，使用 \\times 表示乘号，使用 ^{-1} 表示逆矩阵。\n",
    "- 矩阵的转置：\n",
    "    - 设 A 为 m×n 阶矩阵（即 m 行 n 列），第 i 行 j 列的元素是 a(i,j)，即：A=a(i,j)\n",
    "    - 定义 A 的转置为这样一个 n×m 阶矩阵 B，满足 B=a(j,i)，即 b (i,j)=a (j,i)（B 的第 i 行第 j 列元素是 A 的第 j 行第 i 列元素），记 A^T =B。\n",
    "    - 直观来看，将 A 的所有元素绕着一条从第 1 行第 1 列元素出发的右下方 45 度的射线作 镜面反转，即得到 A 的转置。\n",
    "        $$\n",
    "        A = \\begin{bmatrix}\n",
    "        1 & 2 & 3 \\\\\n",
    "        4 & 5 & 6 \\\\\n",
    "        7 & 8 & 9 \\\\\n",
    "        \\end{bmatrix}\n",
    "        $$\n",
    "        $$\n",
    "        A^T = \\begin{bmatrix}\n",
    "        1 & 4 & 7 \\\\\n",
    "        2 & 5 & 8 \\\\\n",
    "        3 & 6 & 9 \\\\\n",
    "        \\end{bmatrix}\n",
    "        $$"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "16b771a256f4a89d"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5 矩阵运算\n",
    "\n",
    "![](../.images/Snipaste_2024-02-21_15-00-28.png)\n",
    "## 5.1 矩阵乘法api介绍\n",
    "- np.matmul\n",
    "- np.dot"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4a4943b4cfffae92"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[81.8],\n       [81.4],\n       [82.9],\n       [90. ],\n       [84.8],\n       [84.4],\n       [78.6],\n       [92.6]])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]])\n",
    "b = np.array([[0.7], [0.3]])\n",
    "np.matmul(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:07:20.161494700Z",
     "start_time": "2024-02-21T07:07:20.145659100Z"
    }
   },
   "id": "c8c2691ea8abf4d1",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[81.8],\n       [81.4],\n       [82.9],\n       [90. ],\n       [84.8],\n       [84.4],\n       [78.6],\n       [92.6]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a,b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:07:20.179651500Z",
     "start_time": "2024-02-21T07:07:20.163837300Z"
    }
   },
   "id": "26c464b8ace5bf1e",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "- np.matmul和np.dot的区别:\n",
    "> 二者都是矩阵乘法。\n",
    "\n",
    "- np.matmul中禁止矩阵与标量的乘法。\n",
    "- 在矢量乘矢量的內积运算中，np.matmul与np.dot没有区别。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "31e6178b9d4c4bdb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 6 小结\n",
    "- 1.矩阵和向量【知道】\n",
    "    - 矩阵就是特殊的二维数组\n",
    "    - 向量就是一行或者一列的数据\n",
    "- 2.矩阵加法和标量乘法【知道】\n",
    "    - 矩阵的加法:行列数相等的可以加。\n",
    "    - 矩阵的乘法:每个元素都要乘。\n",
    "- 3.矩阵和矩阵(向量)相乘 【知道】\n",
    "    - (M行, N列)*(N行, L列) = (M行, L列)\n",
    "- 4.矩阵性质【知道】\n",
    "    - 矩阵不满足交换率,满足结合律\n",
    "- 5.单位矩阵【知道】\n",
    "    - 对角线都是1的矩阵,其他位置都为0\n",
    "- 6.矩阵运算【掌握】\n",
    "    - np.matmul\n",
    "    - np.dot\n",
    "    - 注意：二者都是矩阵乘法。 np.matmul中禁止矩阵与标量的乘法。 在矢量乘矢量的內积运算中，np.matmul与np.dot没有区别。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "86ceaa0fb0b11689"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:07:20.179651500Z",
     "start_time": "2024-02-21T07:07:20.169112700Z"
    }
   },
   "id": "6236d3d55a058ba1",
   "execution_count": 5
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
